package com.ebocy.hammer.core.domain;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.mina.core.session.IoSession;

import com.ebocy.hammer.core.GameCache;
import com.ebocy.hammer.core.db.annotations.Entity;
import com.ebocy.hammer.core.db.annotations.Ignore;
import com.ebocy.hammer.core.db.model.BaseEntity;
import com.ebocy.hammer.core.message.AbstractS2CMessage;

/**
 *消息发送应封装起来----单独用一发送的接口 2012-4-16@author yangshaobo
 */
@Entity
public class GamePlayer extends BaseEntity {
	private static final long serialVersionUID = 1L;

	public static String GAMEPLAYERID = "gamePlayerId";
	public static String NAME = "name";
	public static String HEADPORTRAIT = "headPortrait";
	public static String LEVEL = "level";
	public static String EXP = "exp";
	public static String SUMPOINTS = "sumPoints";
	public static String GOLD = "gold";
	public static String EGOLD = "eGold";
	public static String SUMPERBATTLETIMES = "sumPerBattleTimes";
	public static String SUMTEAMBATTLETIMES = "sumTeamBattleTimes";
	public static String SUMFIRSTPERBATTLETIMES = "sumFirstPerBattleTimes";
	public static String SUMFIRSTTEAMBATTLETIMES = "sumFirstTeamBattleTimes";
	public static String SUMKILLPERBATTLETIMES = "sumKillPerBattleTimes";
	public static String SUMKILLTEAMBATTLETIMES = "sumKillTeamBattleTimes";
	public static String FRIENDIDS = "friendIds";
	public static String EFFORT = "effort";
	public static String LATENCY = "latency";
	public static String ROLEIMAGE = "roleImage";
	public static String HAMMERID = "hammerId";

	// 玩家id
	private long gamePlayerId;
	// 昵称
	private String name;
	// 玩家头像
	private String headPortrait;
	@Ignore
	// 玩家背包
	private List<GamePlayerItem> knapsack;
	@Ignore
	// 玩家buffer
	private Map<Integer, Buffer> buffer;
	// 玩家等级(1-255)
	private short level;
	// 玩家当前经验(1-99999)
	private int exp;
	// 玩家总积分
	private long sumPoints;
	// 玩家当前金币
	private int gold;
	// 玩家当前e币
	private int eGold;
	// 玩家形象
	private int roleImage;
	// 玩家锤子
	private int hammerId;
	// 玩家参与个人战总场次
	private int sumPerBattleTimes;
	// 玩家参与组队战总场次
	private int sumTeamBattleTimes;
	// 玩家个人战第一名总场次
	private int sumFirstPerBattleTimes;
	// 玩家组队战第一名总场次
	private int sumFirstTeamBattleTimes;
	// 玩家个人战中击杀对手总数
	private int sumKillPerBattleTimes;
	// 玩家组队战中击杀对手总数
	private int sumKillTeamBattleTimes;
	// 玩家好友
	private String friendIds;
	// 玩家成就
	private int effort;

	// TODO----------位置属性 ---应将此类属性单独提取一个 实体类 ---放battleServer
	@Ignore
	private short x;
	@Ignore
	private short y;
	@Ignore
	private byte dir;
	@Ignore
	public short countOfKill; // 击杀数
	@Ignore
	public short countOfDizzy; // 眩晕数
	@Ignore
	public long heartBeatTimestamp; // 最近一次收到心跳的时间

	/**
	 * 组队过程中的teamID
	 */
	@Ignore
	private byte teamId;

	// 网络延迟
	@Ignore
	public int latency;

	// 坠落位置
	@Ignore
	public short[] fallingPosition = new short[2];
	// 击杀玩家ID
	@Ignore
	public long killer = 0;

	/** 所在房间的id */
	@Ignore
	private int roomId;

	@Ignore
	private IoSession session;

	public GamePlayer() {
	}

	public GamePlayer(short level, int exp, long sumPoints, int gold, int eGold, int sumPerBattleTimes, int sumTeamBattleTimes, int sumFirstPerBattleTimes, int sumFirstTeamBattleTimes, int sumKillPerBattleTimes, int sumKillTeamBattleTimes) {
		this.level = level;
		this.exp = exp;
		this.sumPoints = sumPoints;
		this.gold = gold;
		this.eGold = eGold;
		this.sumPerBattleTimes = sumPerBattleTimes;
		this.sumTeamBattleTimes = sumTeamBattleTimes;
		this.sumFirstPerBattleTimes = sumFirstPerBattleTimes;
		this.sumFirstTeamBattleTimes = sumFirstTeamBattleTimes;
		this.sumKillPerBattleTimes = sumKillPerBattleTimes;
		this.sumKillTeamBattleTimes = sumKillTeamBattleTimes;
	}

	/**
	 * 用户数值初始化
	 * 
	 * @return
	 */
	public static GamePlayer init() {
		short level = 1;
		int exp = 0;
		long sumPoints = 0;
		// 测试金币
		int gold = 1000000;
		// int gold = 1000;
		// 测试e币
		int eGold = 10000;
		// int eGold = 0;
		int sumPerBattleTimes = 0;
		int sumTeamBattleTimes = 0;
		int sumFirstPerBattleTimes = 0;
		int sumFirstTeamBattleTimes = 0;
		int sumKillPerBattleTimes = 0;
		int sumKillTeamBattleTimes = 0;
		return new GamePlayer(level, exp, sumPoints, gold, eGold, sumPerBattleTimes, sumTeamBattleTimes, sumFirstPerBattleTimes, sumFirstTeamBattleTimes, sumKillPerBattleTimes, sumKillTeamBattleTimes);
	}

	public byte getTeamId() {
		return teamId;
	}

	public void setTeamId(byte teamId) {
		this.teamId = teamId;
	}

	public short getX() {
		return x;
	}

	public void setX(short x) {
		this.x = x;
	}

	public short getY() {
		return y;
	}

	public void setY(short y) {
		this.y = y;
	}

	public byte getDir() {
		return dir;
	}

	public void setDir(byte dir) {
		this.dir = dir;
	}

	public int getRoomId() {
		return roomId;
	}

	public void setRoomId(int roomId) {
		this.roomId = roomId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getHeadPortrait() {
		return headPortrait;
	}

	public void setHeadPortrait(String headPortrait) {
		this.headPortrait = headPortrait;
	}

	public int getRoleImage() {
		return roleImage;
	}

	public void setRoleImage(int roleImage) {
		this.roleImage = roleImage;
	}

	public int getHammerId() {
		return hammerId;
	}

	public void setHammerId(int hammerId) {
		this.hammerId = hammerId;
	}

	public List<GamePlayerItem> getKnapsack() {
		return knapsack;
	}

	public void setKnapsack(List<GamePlayerItem> knapsack) {
		this.knapsack = knapsack;
	}

	public Map<Integer, Buffer> getBuffer() {
		return buffer;
	}

	public void setBuffer(Map<Integer, Buffer> buffer) {
		this.buffer = buffer;
	}

	public short getLevel() {
		return level;
	}

	public void setLevel(short level) {
		this.level = level;
	}

	public int getExp() {
		return exp;
	}

	public void setExp(int exp) {
		this.exp = exp;
	}

	public long getGamePlayerId() {
		return gamePlayerId;
	}

	public void setGamePlayerId(long gamePlayerId) {
		this.gamePlayerId = gamePlayerId;
	}

	public long getSumPoints() {
		return sumPoints;
	}

	public void setSumPoints(long sumPoints) {
		this.sumPoints = sumPoints;
	}

	public int getGold() {
		return gold;
	}

	public void setGold(int gold) {
		this.gold = gold;
	}

	public int geteGold() {
		return eGold;
	}

	public void seteGold(int eGold) {
		this.eGold = eGold;
	}

	public int getSumPerBattleTimes() {
		return sumPerBattleTimes;
	}

	public void setSumPerBattleTimes(int sumPerBattleTimes) {
		this.sumPerBattleTimes = sumPerBattleTimes;
	}

	public int getSumTeamBattleTimes() {
		return sumTeamBattleTimes;
	}

	public void setSumTeamBattleTimes(int sumTeamBattleTimes) {
		this.sumTeamBattleTimes = sumTeamBattleTimes;
	}

	public int getSumFirstPerBattleTimes() {
		return sumFirstPerBattleTimes;
	}

	public void setSumFirstPerBattleTimes(int sumFirstPerBattleTimes) {
		this.sumFirstPerBattleTimes = sumFirstPerBattleTimes;
	}

	public int getSumFirstTeamBattleTimes() {
		return sumFirstTeamBattleTimes;
	}

	public void setSumFirstTeamBattleTimes(int sumFirstTeamBattleTimes) {
		this.sumFirstTeamBattleTimes = sumFirstTeamBattleTimes;
	}

	public int getSumKillPerBattleTimes() {
		return sumKillPerBattleTimes;
	}

	public void setSumKillPerBattleTimes(int sumKillPerBattleTimes) {
		this.sumKillPerBattleTimes = sumKillPerBattleTimes;
	}

	public int getSumKillTeamBattleTimes() {
		return sumKillTeamBattleTimes;
	}

	public void setSumKillTeamBattleTimes(int sumKillTeamBattleTimes) {
		this.sumKillTeamBattleTimes = sumKillTeamBattleTimes;
	}

	public String getFriendIds() {
		return friendIds;
	}

	public void setFriendIds(String friendIds) {
		this.friendIds = friendIds;
	}

	public int getEffort() {
		return effort;
	}

	public void setEffort(int effort) {
		this.effort = effort;
	}

	public void setSession(IoSession session) {
		this.session = session;
	}

	public long getSessionId() {
		return this.session.getId();
	}

	public void sendMessage(AbstractS2CMessage message) {
		session.write(message.getData());
	}

	public void closeConnection() {
		session.close(false);
	}

	public void sendMessageToRoomOther(AbstractS2CMessage message) {
		Room room = GameCache.getRoom(this.roomId);
		if (room == null)
			return;

		Set<Long> members = room.worlds;
		if (room.state > 0) {
			members = room.battles;
		}
		for (Long member : members) {
			GamePlayer gpl = GameCache.getInstance().getCache(member);
			if (gpl != null) {
				if (gpl.getGamePlayerId() != this.gamePlayerId) {
					gpl.sendMessage(message);
				}
			}
		}
	}

	public void sendMessageToRoom(AbstractS2CMessage message) {
		Room room = GameCache.getRoom(this.roomId);
		Set<Long> members = room.worlds;
		if (room.state > 0) {
			members = room.battles;
		}
		for (Long member : members) {
			GamePlayer gpl = GameCache.getInstance().getCache(member);
			gpl.sendMessage(message);
		}
	}

}
